//
//  CityButtonViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/14.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "CityButtonViewController.h"
#import "CityButtonCell.h"
#import "CityBDetailViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "FanProgress3D.h"
#import <MJRefresh/MJRefresh.h>

#define kSearchUrl  @"http://app.quanjingke.com/api.php?appKey=ad36b8ed4ee6ee55faf7f00adf5d0dab&op=api_search&keyword=%@&userId=505425"


#define kCityUrl  @"http://app.quanjingke.com/api.php?op=api_shootlist&appKey=cb4812e66579dfed965e70063d026d8d&userId=545398&province=%@&sign=1"



@interface CityButtonViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation CityButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = [NSString stringWithFormat:@"%@景点",self.cityName];
    [self loadData];
    
    [self loadTableView];
    
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
   

    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
}
- (void)loadData{
    
//    [FanProgress3D showInView:self.view status:@"加载中,请稍后...."];
    
    
    _dataArray =[[NSMutableArray alloc]init];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    [mgr GET:[NSString stringWithFormat:kCityUrl,[self.cityName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        
        if ([responseObject isKindOfClass:[NSData class]]) {
    
            
            [_dataArray removeAllObjects];
            NSMutableArray *array=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            
            [_dataArray addObjectsFromArray:array];
            
            
            [_tableView reloadData];
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
//        [FanProgress3D dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [FanProgress3D dismissWithStatus:error.localizedDescription afterDelay:3];
        
    }];
    
    
    
}
- (void)loadTableView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"CityButtonCell" bundle:nil] forCellReuseIdentifier:@"CityButtonCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"CityButtonCell";
    CityButtonCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDictionary *dict = nil;
    if (indexPath.row<[_dataArray count]) {
        dict=[_dataArray objectAtIndex:indexPath.row];
    }
    
    

    cell.cityLabel.text = [dict objectForKey:@"address"];
    cell.cityLabel.textColor = [UIColor purpleColor];
    cell.addressLabel.text = [dict objectForKey:@"title"];
    cell.addressLabel.textColor=[UIColor magentaColor];
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CityBDetailViewController *cityVc = [[CityBDetailViewController alloc]init];
    cityVc.detailDict=[_dataArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:cityVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
